﻿Imports Equipment.Core
Imports Equipment.Business

Public Class TimeZoneManager
    Private Const USER_GENERAL_DATE_FORMAT As String = "yyyy-MM-dd hh:mm tt"
    Private Const USER_DATE_FORMAT As String = "yyyy-MM-dd hh:mm tt zz"
    Private Const USER_DATE_TEMPLATE As String = "{0} {1}"
    Private Const UNIVERSAL_TIME_ID As String = "UTC"

    Public Shared Function ConvertToUserTimeFor(ByVal dt As DateTime) As DateTime
        Return ConvertToUserTimeFor(dt, Application.CurrentUserInfo)
    End Function

    Public Shared Function ConvertToUserTimeFor(ByVal dt As DateTime, ByVal forUser As IUserInfo) As DateTime
        Return dt.Add(forUser.TimeZoneOffset)
    End Function

    'Public Shared Function ConvertToUserTimeFor(ByVal dt As DateTime, ByVal forUser As IUserInfo) As DateTime
    '    Dim centralTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(UNIVERSAL_TIME_ID)
    '    Dim userTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(forUser.TimeZoneID)
    '    Return TimeZoneInfo.ConvertTimeToUtc(dt, userTimeZone)
    'End Function

    Public Shared Function ConvertToUserTimeStringFor(ByVal dt As DateTime) As String
        Return ConvertToUserTimeStringFor(dt, True)
    End Function

    Public Shared Function ConvertToUserTimeStringFor(ByVal dt As DateTime, ByVal useAbbreviation As Boolean) As String
        Dim userTime As DateTime = ConvertToUserTimeFor(dt)
        If useAbbreviation Then
            Return String.Format(USER_DATE_TEMPLATE, userTime.ToString(USER_GENERAL_DATE_FORMAT), Application.CurrentUserInfo.TimezoneAbbr)
        Else
            Dim userTimeOffset = Application.CurrentUserInfo.TimezoneOffset
            Return String.Format(USER_DATE_TEMPLATE, userTime.ToString(USER_GENERAL_DATE_FORMAT), If(userTimeOffset.TotalMilliseconds < 0, "-", "+") & userTimeOffset.ToString("hh\:mm"))
        End If
    End Function

    Public Shared Function ConvertToCentralTimeFor(ByVal dt As DateTime) As DateTime
        Return ConvertToCentralTimeFor(dt, Application.CurrentUserInfo)
    End Function

    Public Shared Function ConvertToCustomTimeFor(ByVal dt As DateTime, ByVal offset As TimeSpan) As DateTime
        Return dt.AddMinutes(-offset.TotalMinutes)
    End Function

    Public Shared Function ConvertToCustomTimeFor(ByVal dt As DateTime, ByVal timezoneID As String) As DateTime
        Dim offset = TimeZoneManager.GetOffsetByID(timezoneID)
        Return dt.AddMinutes(offset.TotalMinutes)
    End Function

    Public Shared Function ConvertFromCustomTimeFor(ByVal dt As DateTime, ByVal timezoneID As String) As DateTime
        Dim offset = TimeZoneManager.GetOffsetByID(timezoneID)
        Return dt.AddMinutes(-offset.TotalMinutes)
    End Function


    Public Shared Function ConvertToCentralTimeFor(ByVal dt As DateTime, ByVal forUser As IUserInfo) As DateTime
        Dim userTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(forUser.TimeZoneID)
        Return TimeZoneInfo.ConvertTimeToUtc(dt, userTimeZone)
    End Function

    Public Shared Function ConvertToCentralTimeFor(ByVal dt As DateTime, ByVal userTimeZoneID As String) As DateTime
        Dim userTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(userTimeZoneID)
        Return TimeZoneInfo.ConvertTimeToUtc(dt, userTimeZone)
    End Function

    Public Shared Function GetAbbreviationByID(ByVal id As String) As String
        Dim currTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(id)
        Dim sName As String
        If currTimeZone.StandardName Is Nothing Then
            sName = currTimeZone.StandardName
        Else
            sName = currTimeZone.DaylightName
        End If
        Dim sAbbr As String = String.Empty
        Dim sSplit As String() = sName.Split(" ")

        For Each s As String In sSplit
            If s.Length >= 1 Then
                sAbbr = sAbbr + s.Substring(0, 1)
            End If
        Next

        Return sAbbr

    End Function

    'Public Shared Function GetOffsetByID(ByVal id As String) As TimeSpan
    '    Dim currTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(id)
    '    Return String.Format("{0}{1}", If(currTimeZone.BaseUtcOffset.TotalSeconds >= 0, "+", "-"), currTimeZone.BaseUtcOffset.ToString("hh':'mm"))
    'End Function

    Public Shared Function GetOffsetByID(ByVal id As String) As TimeSpan
        Dim currTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(id)
        Return currTimeZone.BaseUtcOffset
    End Function


End Class
